import {
  getPolicyGetList,
  getPolicyPcCate,
  getNormAllclass,
  getNormGetList,
  getPolicyInfo,
  getNormInfo,
} from "@/assets/api/regulationsApi.js";
import {
  recommend_policy,
  recommend_norm
} from "@/assets/api/quizData";



export const state = () => ({
  accountingTabs: [],
  // 税收
  TTabsList: [{
      name: "首页",
      fieldName: "is_hot",
      pathName: 'index',
      value: 1,
    },
    {
      name: "中央税收法规",
      fieldName: "city_code",
      pathName: 'zyfg',
      value: 1,
      class: [],
      searchFilter: {
        code: '1'
      }
    },
    {
      name: "税收优惠政策",
      fieldName: "type",
      pathName: 'yhzz',
      value: 1,
      class: [],
      searchFilter: {
        type: '1'
      }
    },
    {
      name: "地方税收政策",
      fieldName: "city_code",
      pathName: 'dfff',
      value: -1,
      class: [{
        name: "全部",
        id: -1
      }],
      searchFilter: {
        code: '-1'
      }
    },
    {
      name: "海关综合文件",
      fieldName: "policy_cate_id",
      pathName: 'hgzhwj',
      value: 27,
      class: [{
        name: "海关综合文件"
      }],
      searchFilter: {
        policy_cate_id: '27'
      }
    },
    {
      name: "国际跨境税收",
      fieldName: "policy_cate_id",
      pathName: 'gjkjss',
      value: 20,
      class: [{
        name: "国际跨境税收"
      }],
      searchFilter: {
        policy_cate_id: '20'
      }
    },
    {
      name: "外汇文件",
      fieldName: "policy_cate_id",
      pathName: 'whwj',
      value: 28,
      class: [{
        name: "外汇文件"
      }],
      searchFilter: {
        policy_cate_id: '28'
      }
    },
    {
      name: "12366热点回答",
      fieldName: "policy_cate_id",
      pathName: '12366rdwd',
      value: 32,
      class: [{
        name: "12366热点回答"
      }],
      searchFilter: {
        policy_cate_id: '32'
      }
    },
    {
      name: "政府补助",
      fieldName: "policy_cate_id",
      pathName: 'zfbz',
      value: 30,
      class: [{
        name: "政府补助"
      }],
      searchFilter: {
        policy_cate_id: '30'
      }
    },
    {
      name: "法规法条",
      fieldName: "is_fatiao",
      pathName: 'fgft',
      value: 1,
      class: [{
        name: "法规法条"
      }],
      searchFilter: {
        is_fatiao: '1'
      }
    },
  ],
  ATabsList: [{
      name: '首页',
      id: '86',
      pathName: 'index',
      class: [{
        id: 86
      }]
    },
    {
      id: 1,
      name: '企业会计',
      sort: '99',
      pathName: 'qykj',
      class: []
    },
    {
      id: 2,
      name: '证监会',
      sort: '0',
      pathName: 'zjh',
      class: []
    },
    {
      id: 3,
      name: '政府会计',
      sort: '98',
      pathName: 'zfkj',
      class: []
    },
    {
      id: 4,
      name: '国际会计',
      sort: '0',
      pathName: 'gjkj',
      class: []
    },
    {
      id: 5,
      name: '企业内控',
      sort: '96',
      pathName: 'qynk',
      class: []
    },
    {
      id: 6,
      name: '全行业会计核算',
      sort: '97',
      pathName: 'qhykjhs',
      class: []
    },
    {
      id: 7,
      name: '其他会计法规',
      sort: '0',
      pathName: 'qtkjfg',
      class: []
    },
    {
      id: 8,
      name: '全科目核算',
      sort: '96.5',
      pathName: 'qkmhs',
      class: []
    }
  ],
  // 税种
  TPolicyPcCate: [],
  // 数据
  TDataList: null,
  ADataList: [],

  // 税收推荐阅读
  ThotReadList: [],
  TreadingList: [],

  // 会计推荐阅读
  AhotReadList: [],
  AreadingList: [],

  infoData: null,
  hotReadList: [],
  readingList: [],

  ADinfoData: null,
  ADhotReadList: [],
  ADreadingList: [],

})

export const getters = {
  accountingTabs: state => state.accountingTabs,
  TPolicyPcCate: state => state.TPolicyPcCate,
  TDataList: state => state.TDataList,
  TTabsList: state => state.TTabsList,
  chinatax: state => state.chinatax,
  ATabsList: state => state.ATabsList,
  ADataList: state => state.ADataList,

  ThotReadList: state => state.ThotReadList,
  TreadingList: state => state.TreadingList,

  AhotReadList: state => state.AhotReadList,
  AreadingList: state => state.AreadingList,

  infoData: state => state.infoData,
  hotReadList: state => state.hotReadList,
  readingList: state => state.readingList,

  ADinfoData: state => state.ADinfoData,
  ADhotReadList: state => state.ADhotReadList,
  ADreadingList: state => state.ADreadingList,

}

export const mutations = {

  setaccountingTabs(state, accountingTabs) {
    state.accountingTabs = accountingTabs;
  },
  set_TPolicyPcCate(state, data) {
    state.TPolicyPcCate = data;
  },
  set_TDataList(state, data) {
    state.TDataList = data;
  },
  set_TTabsList(state, data) {
    state.TTabsList[1].class = data;
    state.TTabsList[2].class = data;
  },
  set_ATabsList(state, data) {
    state.ATabsList = data;
  },
  set_ADataList(state, data) {
    state.ADataList = data;
  },

  set_ThotReadList(state, data) {
    state.ThotReadList = data;
  },
  set_TreadingList(state, data) {
    state.TreadingList = data;
  },

  set_AhotReadList(state, data) {
    state.AhotReadList = data;
  },
  set_AreadingList(state, data) {
    state.AreadingList = data;
  },

  set_TDataInfo(state, data) {
    state.infoData = data.infoData;
    state.hotReadList = data.hotReadList;
    state.readingList = data.readingList;
  },

  set_ADataInfo(state, data) {
    state.ADinfoData = data.infoData;
    state.ADhotReadList = data.hotReadList;
    state.ADreadingList = data.readingList;
  },
}

export const actions = {
  async init({
    commit,
    state
  }, params2) {
    if (state.TTabsList[3].class.length == 1)
      state.TTabsList[3].class.push(...state.chinatax)

    // let tabsIndex = 0
    // let tabsTwoIndex = 0
    // if (params2.tabsIndex)
    //     tabsIndex = params2.tabsIndex;
    // if (params2.tabsTwoIndex)
    //     tabsTwoIndex = params2.tabsTwoIndex;

    const res = await getPolicyPcCate({})
    if (res.code == 1) {
      let list = res.data;
      list.unshift({
        name: "全部",
        id: 0
      });
      // let list = [{ name: '中央法规', id: 1 }, ...res.data]
      for (let i = res.data.length - 1; i >= 0; i--) {
        if (list[i].name.length > 6) {
          list.splice(res.data.length - 2, 0, list[i]);
          list.splice(i, 1);
        }
      }

      commit('set_TTabsList', list)
    }
    // let parems = {
    //     page: 1,
    //     limit: 30,
    //     type: 2,
    // };
    // let tabsList = state.TTabsList
    // parems[tabsList[tabsIndex].fieldName] =
    //     tabsList[tabsIndex].value;
    // if (tabsIndex == 1) {
    //     parems.place = 1;
    //     parems.policy_cate_id = tabsList[1].class[
    //             tabsTwoIndex
    //         ].id ?
    //         tabsList[1].class[tabsTwoIndex].id :
    //         "";
    // }
    // if (tabsIndex == 2) {
    //     parems.policy_cate_id = tabsList[2].class[
    //             tabsTwoIndex
    //         ].id ?
    //         tabsList[2].class[tabsTwoIndex].id :
    //         "";
    //     parems.type = 1;
    // }
    // if (tabsIndex == 3) {
    //     parems.place = 1;
    //     parems.city_code = tabsList[3].class[tabsTwoIndex].id;
    // }

    // const res1 = await getPolicyGetList(parems);
    // if (res1.code == 1) {
    //     commit('set_TDataList', res1)
    // }

    // // 获取推荐阅读
    // const res2 = await recommend_policy({
    //     limit: 10,
    // });
    // if (res2.code == 1) {
    //     commit('set_ThotReadList', res2.data)
    // }
    // const res3 = await recommend_policy({
    //     limit: 20,
    // });
    // if (res3.code == 1) {
    //     commit('set_TreadingList', res3.data)
    // }
  },

  async ASTabList({
    commit,
    state
  }) {
    const res = await getNormAllclass({});
    if (res.code == 1) {
      let list = state.ATabsList
      for (let i = 1; i < list.length; i++) {
        list[i].class = res.data[i + ''].twoclass

      }
      commit('set_ATabsList', list)
    }
  },

  async AInit({
    commit,
    state
  }, params2) {

    let tabsIndex = 0
    let tabsTwoIndex = 0
    if (params2.tabsTwoIndex)
      tabsTwoIndex = params2.tabsTwoIndex;
    if (params2.tabsIndex)
      tabsIndex = params2.tabsIndex;
    // 获取分类
    const res = await getNormAllclass({});
    if (res.code == 1) {
      let list = state.ATabsList
      for (let i = 1; i < list.length; i++) {
        list[i].class = res.data[i + ''].twoclass
        if (params2.type && list[i].pathName == params2.type) {
          tabsIndex = i
        }
      }
      commit('set_ATabsList', list)
    }
    // let page = 1
    // if (params2.page)
    //     page = params2.page
    // const res2 = await getNormGetList({
    //     page: page,
    //     pagesize: 30,
    //     id: state.ATabsList[tabsIndex].class[tabsTwoIndex].id,
    // });
    // if (res2.code == 1) {
    //     commit('set_ADataList', res2.data)
    // }

    // // 获取推荐阅读
    // const res4 = await recommend_norm({
    //     limit: 10,
    // });
    // if (res4.code == 1) {
    //     commit('set_AhotReadList', res4.data)
    // }
    // const res5 = await recommend_norm({
    //     limit: 20,
    // });
    // if (res5.code == 1) {
    //     commit('set_AreadingList', res5.data)
    // }

  },

  async TDetailInit({
    commit,
    state
  }, params) {
    // const res1 = await getPolicyInfo({
    //     id: params.id
    // })
    // const res2 = await recommend_policy({
    //     limit: 10
    // })
    const res3 = await recommend_policy({
      limit: 20
    })
    commit('set_TDataInfo', {
      infoData: null,
      hotReadList: null,
      readingList: res3.data
    })
  },

  async TzyfgDetailInit({
    commit,
    state
  }, params) {
    // const res1 = await getPolicyInfo({
    //     id: params.id,
    //     type: 1
    // })
    // const res2 = await recommend_policy({
    //     limit: 10
    // })
    const res3 = await recommend_policy({
      limit: 20
    })
    commit('set_TDataInfo', {
      infoData: null,
      hotReadList: null,
      readingList: res3.data
    })
  },

  async ADetailInit({
    commit,
    state
  }, params) {
    // const res1 = await getNormInfo({
    //     id: params.id
    // })
    // const res2 = await recommend_norm({
    //     limit: 10
    // })
    const res3 = await recommend_norm({
      limit: 20
    })
    commit('set_ADataInfo', {
      infoData: null,
      hotReadList: null,
      readingList: res3.data
    })
  },


}
